package com.service.medical.Impl;

import com.comm.R;
import com.entity.DrugAll;
import com.entity.DrugOrder;
import com.github.pagehelper.PageHelper;
import com.mapper.medical.StockMapper;
import com.service.medical.StockService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StockServiceImpl implements StockService {

    @Resource
    private StockMapper stockMapper;

    @Override
    public R findAllDrug(int currentPage,int pageSize,String drugName) {
        R r = new R();
        try {
            com.github.pagehelper.Page<DrugAll> page = PageHelper.startPage(currentPage,pageSize);
            List<DrugAll> drugAllList = stockMapper.findAllDrug(drugName);
            long count =  page.getTotal();
            System.out.println("总记录数:"+count);
            if(count>0){
                r.setCode(200);
                r.setData(drugAllList);
            }else {
                r.setCode(500);
                r.setData("查询失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");
        }
        return r;
    }

    @Override
    public R findDrugByName(String drugName) {
        R r = new R();
        try {
            List<DrugAll> drugListByName = stockMapper.findDrugByName(drugName);
            System.out.println(drugListByName.size());
            int count = drugListByName.size();
            if(count>0){
                r.setCode(200);
                r.setData(drugListByName);
            }else {
                r.setCode(500);
                r.setData("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");
        }
        return r;
    }

    @Override
    public R updatePrice(String drugName,String allStock, int price) {
        R r = new R();
        try {
            Integer result = stockMapper.updatePrice(drugName,allStock,price);
            if(result>0){
                r.setCode(200);
                r.setData("success");
            }else {
                r.setCode(500);
                r.setData("调价失败");
            }

        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");

        }
        return r;
    }

    @Override
    public R findAllOrder(int currentPage,int pageSize,String drugName) {
        R r = new R();
        try {
            com.github.pagehelper.Page<DrugOrder> page = PageHelper.startPage(currentPage,pageSize);
            List<DrugOrder> drugAllList = stockMapper.findAllOrder(drugName);
            long count =  page.getTotal();
            System.out.println("总记录数:"+count);
            if(count>0){
                r.setCode(200);
                r.setData(drugAllList);
            }else {
                r.setCode(500);
                r.setData("查询失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");
        }
        return r;
    }
}
